package com.onpositive.gamemaker.treeview;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 * This class provides the content for the tree in FileTree
 */

class FileTreeContentProvider implements ITreeContentProvider {
	
	protected File root;
	protected Comparator<? super File> dirComparator = new Comparator<File>()
	{

		public int compare(File file1, File file2)
		{
			boolean firstDir = file1.isDirectory();
			boolean secondDir = file2.isDirectory();
			if (firstDir && secondDir || !(firstDir || secondDir))
				return file1.getName().compareToIgnoreCase(file2.getName());
			if (firstDir)
				return -1;
			if (secondDir)
				return 1;
			return 0;
		}

		
	};
	
  /**
   * Gets the children of the specified object
   * 
   * @param arg0
   *            the parent object
   * @return Object[]
   */
  public Object[] getChildren(Object arg0) {
    // Return the files and subdirectories in this directory
    File[] filesList = ((File) arg0).listFiles();
    if (filesList != null)
    	Arrays.sort(filesList, dirComparator );
	return filesList;
  }

  /**
   * Gets the parent of the specified object
   * 
   * @param arg0
   *            the object
   * @return Object
   */
  public Object getParent(Object arg0) {
    // Return this file's parent file
    return ((File) arg0).getParentFile();
  }

  /**
   * Returns whether the passed object has children
   * 
   * @param arg0
   *            the parent object
   * @return boolean
   */
  public boolean hasChildren(Object arg0) {
    // Get the children
    Object[] obj = getChildren(arg0);
    
    // Return whether the parent has children
    return obj == null ? false : obj.length > 0;
  }

  /**
   * Gets the root element(s) of the tree
   * 
   * @param arg0
   *            the input data
   * @return Object[]
   */
  public Object[] getElements(Object arg0) {
    // These are the root elements of the tree
    // We don't care what arg0 is, because we just want all
    // the root nodes in the file system
	File[] filesList = root.listFiles();
	Arrays.sort(filesList, dirComparator);
	return filesList;
  }

  /**
   * Disposes any created resources
   */
  public void dispose() {
    // Nothing to dispose
  }

  /**
   * Called when the input changes
   * 
   * @param arg0
   *            the viewer
   * @param arg1
   *            the old input
   * @param arg2
   *            the new input
   */
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
	  if (newInput instanceof File)
		  root = (File) newInput;
	  else if (newInput instanceof String)
	  {
		  File file = new File((String) newInput);
		  if (file.exists() && file.isDirectory())
			  root = file;
	  }
  }
}
